//
//  TableView.swift
//  MYModule
//
//  Created by 李然 on 2022/7/19.
//

import UIKit

open class TableView: UITableView {
    
    /// 是否自动适应 contentSize 调整自动布局宽高
   public var automaticallyContentSize: Bool = false

    public override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        makeUI()
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        makeUI()
    }

    open func makeUI() {
        rowHeight = UITableView.automaticDimension
        estimatedRowHeight = 50
        sectionHeaderHeight = 0
        backgroundColor = .clear
        keyboardDismissMode = .onDrag
        separatorColor = .clear
        separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        tableFooterView = UIView()
        
        if #available(iOS 15.0, *) {
            sectionHeaderTopPadding = 0.0
        } 
    }
    
    open override var contentSize: CGSize {
        
        didSet {
           
            if !automaticallyContentSize {
                return
            }
        
            invalidateIntrinsicContentSize()
        }
    }
    
    open override var intrinsicContentSize: CGSize {
      var size = super.intrinsicContentSize
        if !automaticallyContentSize {
            return size
        }
        
        layoutIfNeeded()
        size = CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
        return size
        
    }
}
